package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.commons.PageInfo;

import vo.SendMessage;


public class MessageDaoImpl extends BaseDao implements MessageDao {

	@Override
	public void send(SendMessage message, Integer[] userid) {
		//生成发送消息的主键
		SendMessage m=null;
		Connection conn=null;
		PreparedStatement ps=null;
		 ResultSet rs=null;
		try {
			conn=this.getConnection();
			conn.setAutoCommit(false);
			ps=conn.prepareStatement("select max(sendid)+1 sendid from sendmessage");
			rs=ps.executeQuery();
			while(rs.next()) {
				m=new SendMessage();
				m.setSendid(rs.getInt("sendid"));
				m.setUserid(rs.getInt("userid"));
				m.setMessageTitle(rs.getString("messageTitle"));
				m.setMessageContent(rs.getString("messageContent"));
				m.setCreateDate(rs.getDate("createDate"));
				m.setMessageState(rs.getInt("messageState"));
				m.setPriority(rs.getInt("priority"));
			}
			//向发送消息表中插入一条记录
			this.executeSql("INSERT INTO `sendmessage` (`sendid`, `userid`, `messagetitle`, `messagecontent`, `createdate`, `priority`, `messagestate`) VALUES (?, ?, ?, ?, now(), ?, 1)", m.getSendid(),message.getUserid(),message.getMessageTitle(),message.getMessageContent(),message.getPriority());
			//向接收消息表中添加多条记录
			for(Integer i:userid) {
				this.executeSql("INSERT INTO `receivemessage` (`receiveid`, `sendid`, `userid`, `messagestate`) VALUES (null, ?, ?, '1')", m.getSendid(),i);
			}
			conn.commit();
		}catch(Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally {
			this.close(rs);
			this.close(ps);
			this.close(conn);
		}
		System.out.println(m.getSendid());
		
		
	}

	@Override
	public List<Map<String,Object>> getReceiveMessage(Integer userid,PageInfo info) {
		
		List<Map<String,Object>> list=this.executeQuery("select receiveid,u.`name` username,`messagetitle`,r.`messagestate` messagestate,r.`opendate`,\n" + 
				"case\n" + 
				"	when r.`messagestate`=1 then '未读'\n" + 
				"	when r.`messagestate`=2 then '已读'\n" + 
				"end state\n" + 
				" from `receivemessage` r left join `sendmessage` s on r.sendid=s.sendid left join tb_user u on u.`userid`=s.`userid` where r.`userid`=? and r.`messagestate`!=3", userid);
		
		info.setRecordCount(list.size());
		info.setList(list);
		return list;
	}

	@Override
	public List<Map<String, Object>> getSendMessage(Integer userid,PageInfo info) {
		List<Map<String,Object>> list=this.executeQuery("select sendid,messagetitle,createdate,'' username from sendmessage where userid=? and messagestate!=2 order by createdate desc", userid);
		for(Map<String,Object> map:list){
			List<Map<String,Object>> names=this.executeQuery("select name from tb_user where userid in(select userid from receivemessage where sendid=?)", map.get("sendid"));
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<names.size();i++) {
				if(i==names.size()-1) {
					sb.append(names.get(i).values().toString());
				}else {
					sb.append(names.get(i).values().toString()+"，");
				}
			}
			map.put("receiver", sb.toString());
		}
		
		info.setRecordCount(list.size());
		info.setList(list);
		return list;
	}

}
